package com.pur.zsms.ordercustommanage.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderCustFoodPo;
import com.pur.zsms.ordercustommanage.po.OrderCustPo;
import com.pur.zsms.ordercustommanage.service.ISelectFoodService;
import com.pur.zsms.ordercustommanage.service.InsertCusFoodService;
import com.pur.zsms.ordercustommanage.vo.OrderCustVo;
import com.pur.zsms.ordercustommanage.vo.SelectFoodVo;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;

@Service("insertCusFoodService")
public class InsertCusFoodServiceImpl implements InsertCusFoodService {

	@Resource(name = "selectFoodDao")
	private ISelectFoodDao selectFoodDaoImpl;

	public ISelectFoodDao getSelectFoodDaoImpl() {
		return selectFoodDaoImpl;
	}

	public void setSelectFoodDaoImpl(ISelectFoodDao selectFoodDaoImpl) {
		this.selectFoodDaoImpl = selectFoodDaoImpl;
	}

	@Override
	public void addOrderFood(List<SelectFoodVo> selectFoodList, UserPo po, int ordId) throws GenericException {
		for (int i = 0; i < selectFoodList.size(); i++) {
			OrderCustFoodPo custPo = new OrderCustFoodPo();
			custPo.setOrdId(ordId);
			custPo.setOrdFoodId(selectFoodList.get(i).getFoodId());
			custPo.setOrdFoodNum(selectFoodList.get(i).getFoodNum());
			// 价钱计算
			BigDecimal value = selectFoodDaoImpl.getFootValueByFoodId(selectFoodList.get(i).getFoodId(), (int) po.getUserId());
			BigDecimal fraction = new BigDecimal(selectFoodList.get(i).getFoodNum());
			custPo.setOrdFoodValue(value.multiply(fraction));

			custPo.setOrdMaterId(0);
			custPo.setOrdMaterNum(new BigDecimal(0));
			custPo.setOrdMaterValue(new BigDecimal(0));
			custPo.setOrdMaterLevel(0);
			selectFoodDaoImpl.addOrderFood(custPo);
		}
	}

	@Override
	public void addMaterOrderFood(List<SelectMaterVo> selectMaterList, int ordId) throws GenericException {
		for (int i = 0; i < selectMaterList.size(); i++) {
			OrderCustFoodPo custPo = new OrderCustFoodPo();
			custPo.setOrdId(ordId);
			custPo.setOrdMaterId(selectMaterList.get(i).getMaterId());
			custPo.setOrdMaterNum(selectMaterList.get(i).getMaterNum());
			custPo.setLeaveMessage(selectMaterList.get(i).getLeaveMessage());
			custPo.setOrdMaterLevel(Integer.valueOf(selectMaterList.get(i).getMaterLevel()));
			// 价钱计算
			BigDecimal value = selectFoodDaoImpl.getMaterLevelByMaterIdAndLevel(selectMaterList.get(i).getMaterId(),
					selectMaterList.get(i).getMaterLevel()).getMaterValueUndressed();
			BigDecimal fraction = selectMaterList.get(i).getMaterNum();
			custPo.setOrdMaterValue(value.multiply(fraction).multiply(new BigDecimal("1000")));
			custPo.setOrdFoodId(0);
			custPo.setOrdFoodNum(0);
			custPo.setOrdFoodValue(new BigDecimal(0));
			custPo.setSubmitOrdTime(selectMaterList.get(i).getSubmitOrdTime());
			selectFoodDaoImpl.addOrderFood(custPo);
		}

	}

	@Override
	public void addMaterOrderFoodForSelfForunusual(List<SelectMaterVo> selectMaterList, UserPo po, int ordId) throws GenericException {
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();
		for (int i = 0; i < selectMaterList.size(); i++) {
			OrderCustFoodPo custPo = new OrderCustFoodPo();
			custPo.setOrdId(ordId);
			custPo.setOrdMaterId(selectMaterList.get(i).getMaterId());
			custPo.setOrdMaterNum(selectMaterList.get(i).getMaterNum());
			custPo.setOrdMaterLevel(Integer.valueOf(selectMaterList.get(i).getMaterLevel()));
			custPo.setOrdMaterValue(selectMaterList.get(i).getMaterValueUndressed().multiply(selectMaterList.get(i).getMaterNum()));
			custPo.setOrdFoodId(0);
			custPo.setOrdFoodNum(0);
			custPo.setOrdFoodValue(new BigDecimal(0));
			custPo.setSubmitOrdTime(nowTime);
			selectFoodDaoImpl.addOrderFood(custPo);
		}

	}

	public void addMaterOrderFoodForSelf(List<SelectMaterVo> selectMaterList, long shopId, int ordId) throws GenericException {
		for (int i = 0; i < selectMaterList.size(); i++) {
			OrderCustFoodPo custPo = new OrderCustFoodPo();
			custPo.setOrdId(ordId);
			custPo.setOrdMaterId(selectMaterList.get(i).getMaterId());
			custPo.setOrdMaterNum(selectMaterList.get(i).getMaterNum());
			custPo.setOrdMaterLevel(Integer.valueOf(selectMaterList.get(i).getMaterLevel()));
			custPo.setOrdMaterValue(selectMaterList.get(i).getMaterValueAll());
			custPo.setOrdFoodId(0);
			custPo.setOrdFoodNum(0);
			custPo.setOrdFoodValue(new BigDecimal(0));
			selectFoodDaoImpl.addOrderFood(custPo);
		}
	}

	@Override
	public void updateMaterOrderFood(List<SelectMaterVo> selectMaterList, int ordId) throws GenericException {
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();
		for (int i = 0; i < selectMaterList.size(); i++) {
			// 和第一次录入的做对比
			List<OrderCustFoodPo> orderCustFoodPoList = selectFoodDaoImpl.getOrderCustFoodByOrdIdMaterIdMaterLevel(ordId, selectMaterList.get(i)
					.getMaterId(), Integer.valueOf(selectMaterList.get(i).getMaterLevel()));

			OrderCustFoodPo custPo = new OrderCustFoodPo();
			if (orderCustFoodPoList != null && orderCustFoodPoList.size() > 0) {
				custPo = orderCustFoodPoList.get(0);
				custPo.setOrdMaterNum(custPo.getOrdMaterNum().add(selectMaterList.get(i).getMaterNum()));

				// 价钱计算
				if (custPo.getOrdMaterValue() == null) {
					custPo.setOrdMaterValue(new BigDecimal(0));
				}
				custPo.setOrdMaterValue((custPo.getOrdMaterValue().divide(custPo.getOrdMaterNum(), 2, BigDecimal.ROUND_HALF_UP))
						.multiply(orderCustFoodPoList.get(0).getOrdMaterNum().add(selectMaterList.get(i).getMaterNum())));
				custPo.setOrdMaterValue(new BigDecimal(0));

				custPo.setOrdFoodId(0);
				custPo.setOrdFoodNum(0);
				custPo.setOrdFoodValue(new BigDecimal(0));

				custPo.setLeaveMessage(selectMaterList.get(i).getLeaveMessage());
				custPo.setSubmitOrdTime(nowTime);
				selectFoodDaoImpl.updateOrderFood(custPo);

			} else {

				custPo.setOrdId(ordId);
				custPo.setOrdMaterId(selectMaterList.get(i).getMaterId());
				custPo.setOrdMaterNum(selectMaterList.get(i).getMaterNum());
				custPo.setOrdMaterLevel(Integer.valueOf(selectMaterList.get(i).getMaterLevel()));
				// 价钱计算
				custPo.setOrdMaterValue(selectMaterList.get(i).getMaterValue());
				custPo.setOrdFoodId(0);
				custPo.setOrdFoodNum(0);
				custPo.setOrdFoodValue(new BigDecimal(0));
				custPo.setLeaveMessage(selectMaterList.get(i).getLeaveMessage());
				custPo.setSubmitOrdTime(nowTime);
				selectFoodDaoImpl.addOrderFood(custPo);

			}
		}
	}

	@Override
	public void updateMaterOrderFoodForSelf(List<SelectMaterVo> selectMaterList, int ordId) throws GenericException {
		String nowTime = PubFun.getCurrentDate() + " " + PubFun.getCurrentTime();
		for (int i = 0; i < selectMaterList.size(); i++) {
			// 和第一次录入的做对比
			List<OrderCustFoodPo> orderCustFoodPoList = selectFoodDaoImpl.getOrderCustFoodByOrdIdMaterIdMaterLevel(ordId, selectMaterList.get(i)
					.getMaterId(), Integer.valueOf(selectMaterList.get(i).getMaterLevel()));

			OrderCustFoodPo custPo = new OrderCustFoodPo();

			if (orderCustFoodPoList != null && orderCustFoodPoList.size() > 0) {
				custPo = orderCustFoodPoList.get(0);
				custPo.setOrdMaterNum(custPo.getOrdMaterNum().add(selectMaterList.get(i).getMaterNum()));
				// 价钱计算
				custPo.setOrdMaterValue(custPo.getOrdMaterNum().multiply(selectMaterList.get(i).getMaterValueUndressed()));
				custPo.setOrdFoodId(0);
				custPo.setOrdFoodNum(0);
				custPo.setOrdFoodValue(new BigDecimal(0));

			} else {

				custPo.setOrdId(ordId);
				custPo.setOrdMaterId(selectMaterList.get(i).getMaterId());
				custPo.setOrdMaterNum(selectMaterList.get(i).getMaterNum());
				custPo.setOrdMaterLevel(Integer.valueOf(selectMaterList.get(i).getMaterLevel()));
				// 价钱计算
				custPo.setOrdMaterValue(selectMaterList.get(i).getMaterValueAll());
				custPo.setOrdFoodId(0);
				custPo.setOrdFoodNum(0);
				custPo.setOrdFoodValue(new BigDecimal(0));
			}
			custPo.setSubmitOrdTime(nowTime);
			selectFoodDaoImpl.addOrderFood(custPo);
		}
	}

}
